<template>
	<view class="byt-aidialogue" :class="{ 'is-default-bg': !config.main.background }" :style="aidialogueStyle">
		<!-- 头部 -->
		<Navbar ref="navbar" :title="agentNameAgent" bgColor="transparent" :backShow="backShow" :id="agentIdAgent" :dropdownOptions="dropdownOptions" :defaultDropdownValue="defaultDropdownValue" :borShow="false"
            :userData="userData" :avatar="config.roles.user.avatar"
            @select-conversation="handleSelectConversation" @dropdown-change="handleDropdownChange" @resetconv="resetconv" @dropdown-click="getPublished(true)">
			<slot></slot>
		</Navbar>
		<!-- 主体 -->
		<view class="byt-aidialogue-bg">
			<!-- 背景角色 -->
<!--			<image v-if="config.main.role" class="byt-aidialogue-bg__role"-->
<!--				:src="`https://img.yummall.cn/baiyetong/byt-uni-ui/role_${config.main.role}.svg`"></image>-->
			<!-- 欢迎栏 -->

			<view class="byt-aidialogue-header" :style="{ background: config.startup.backgroundColor}" v-if="config.startup.welcomeShow">
				<Welcome :colorIcon="colorIconAgent" class="byt-aidialogue-bg--float" :show="timerShow"
					:description="config.startup.tip" :descriptionTitle="config.startup.tipTitle" :icon="config.startup.avatar"
					:rightIcon="config.startup.rightIcon" :rotateIcon="config.startup.rotateIcon"></Welcome>
<!--				<transition name="fade">-->
<!--					<Prompts :colorIcon="colorIconAgent" @itemClick="problemClick"-->
<!--						v-if="showRecommendedMessages && config.guess" :aiAgentId="agentIdAgent"-->
<!--						class="recommended-messages"></Prompts>-->

<!--				</transition>-->
			</view>
			<!-- 消息列表 -->
			<view class="byt-aidialogue-content byt-aidialogue-bg--float" :style="{
                    height: `calc(100% - 50px - 210rpx - env(safe-area-inset-bottom) - ${statusBarHeight + 'px'} - ${moreShow ? '100px' : '0px'})`,
                    transition: 'all 0.2s linear'
                }">
				<BubbleList :colorIcon="colorIconAgent" :typeWhiteList="config.typeWhiteList" :roles="config.roles"
					:items="bubbleItems" :scrollTop="scrollTop" @scrolltoupper="onPull" :status="recordLoading"
					 :loading="answerStatus" :senderHeight="senderHeight">
					<template v-slot:content="{ type }">
					</template>
					<template #footer>
						<view v-if="resizeBtnShow"
							style="display: flex; flex-direction: row; justify-content: center; align-items: center; margin-top: 24rpx">
							<basic-icon size="32rpx" style="height: 32rpx"
								:name="colorIconAgent.refreshIcon ? colorIconAgent.refreshIcon : '/static/refresh.svg'"></basic-icon>
							<text @click="wxResize"
								:style="{ color: colorIconAgent.textMainColor ? colorIconAgent.textMainColor : '#1890ff', 'margin-left': '8rpx' }">
								重新链接
							</text>
						</view>
					</template>
				</BubbleList>
			</view>
      <view>
        <uv-popup ref="popup"
                  mode="bottom"
                  @change="popchange"
                  round="16"
                  :closeOnClickOverlay="false"
                  :safeAreaInsetBottom="true"
        >
          <view :style="{'height': this.windowHeight}">
            <uni-row>
            <view style="padding: 24rpx;width: 100%">
              <uv-icon name="close-circle" @click="popClose" size="24" style="float: right"></uv-icon>
            </view>
            </uni-row>
            <view class="b-popup">
              <text :selectable="true">
              {{popContent}}
              </text>
            </view>
          </view>
        </uv-popup>
        <uv-modal ref="deletemodal" title="确认删除？" @confirm="deleteconfirm" :showCancelButton="true">
        </uv-modal>
      </view>
			<!-- 输入框 -->
			<view>
				<!-- #ifdef VUE2 -->
				<Sender id="sender-component" :colorIcon="colorIconAgent" :loading="answerStatus" :audioShow="config.audio"
					:quickShow="config.quickShow" :moreListShow="config.moreListShow" :microphone.sync="microphone"
					:message.sync="message" @focus="onFocus" @submit="setMessage" :slotTopShow="toolListShow"
					:moreShow.sync="moreShow" :moreList="moreListAgent" @ocr="handleToolListClick">
					<template v-if="toolListShow" #top>
						<ToolList :colorIcon="colorIconAgent" :items="toolListAgent" @clickToolList="handleToolListClick">
						</ToolList>
					</template>
				</Sender>
				<!-- #endif -->
				<!-- #ifdef VUE3 -->
				<Sender id="sender-component" :colorIcon="colorIconAgent" :loading="answerStatus" :audioShow="config.audio"
					:quickShow="config.quickShow" :moreListShow="config.moreListShow" v-model:microphone="microphone"
					v-model:message="message" @focus="onFocus" @submit="setMessage" :slotTopShow="toolListShow"
					v-model:moreShow="moreShow" :moreList="moreListAgent" @ocr="handleToolListClick">
					<template v-if="toolListShow" #top>
						<ToolList :colorIcon="colorIconAgent" :items="toolListAgent" @clickToolList="handleToolListClick">
						</ToolList>
					</template>
				</Sender>
				<!-- #endif -->
			</view>
		</view>
	</view>
</template>

<script>
	import {
		Group,
		isJsonString,
		getCurrentDateTime,
		isEmpty,
		showToast,
		debounce
	} from '../../../utils/index.js';
  import {
    chatClearMessage,
    chatGuessProblem,
    chatHistoryList,
    chatOpenNewHistory,
    chatTechnologyDetail,
    getDefaultAgent,
    getInfo,
    chat,
    published,
    messages, addConversation, buttons, updateConversation, delete_message
  } from '../../../common/api.js';

	// import { BasicButton, BasicIcon, BasicLoadmore } from '../../basic';
	// 按钮
	import BasicButton from '../../basic/basic-button/basic-button.vue';
	// 图标
	import BasicIcon from '../../basic/basic-icon/basic-icon.vue';
	// 加载更多
	import BasicLoadmore from '../../basic/basic-loadmore/basic-loadmore.vue';
	// import { Navbar, Welcome, Prompts, Bubble, Sender, BubbleList } from '../../core';
	// 猜你想问
	import Prompts from '../../core/Prompts/Prompts.vue';
	// 对话气泡组件
	import Bubble from '../../core/Bubble/Bubble.vue';
	// 对话气泡组件列表
	import BubbleList from '../../core/BubbleList/BubbleList.vue';
	// 欢迎语
	import Welcome from '../../core/Welcome/Welcome.vue';
	// 头部标题组件
	import Navbar from '../../core/Navbar/Navbar.vue';
	// 输入框
	import Sender from '../../core/Sender/Sender.vue';
	// 工具列表组件
	import ToolList from '../../core/ToolList/ToolList.vue';

	import baseConfig from '../../../common/config.js';
  import {address} from "@/config/conf";
  import {verifyImageUrl} from "@/utils/utils";
	const {
		wsUrl,
		wsChartPath,
		getToken
	} = baseConfig;

	export default {
		components: {
			BasicIcon,
			BasicButton,
			BasicLoadmore,
			Welcome,
			Prompts,
			Bubble,
			BubbleList,
			Navbar,
			Sender,
			ToolList
		},
		props: {
			config: {
				type: Object,
				default: () => ({
					// 颜色和图标
					colorIcon: {
						// 主题色
						mainColor: '#448ef7',
						// 辅助色
						assistColor: '#a8ecff',
						// 字体主色
						textMainColor: '#1890ff',
						// 渐变色
						linearColor: '#27c9ff,#a8ecff',

						// 用户消息字体颜色
						userTextColor: '#FFFFFF',
						// 用户消息背景色
						userBgColor: '#448ef7',

						// 做活动按钮图标
						actionIcon: 'https://img.yummall.cn/baiyetong/byt-uni-ui/action.svg',
						// 复制按钮图标
						copyIcon: 'https://img.yummall.cn/baiyetong/byt-uni-ui/copy.svg',
						// 重新提问图标
						refreshIcon: 'https://img.yummall.cn/baiyetong/byt-uni-ui/refresh.svg',
            //分享图标
            shareIcon: '/static/share.svg',

						// 猜你想问背景颜色
						guessBgColor: 'url(https://img.yummall.cn/baiyetong/byt-uni-ui/whiteMengDefauld.svg)',
						// 猜你想问换一换 字体颜色
						guessChangeColor: '#1890ff',
						// 猜你想问换一换图标
						guessChange: 'https://img.yummall.cn/baiyetong/byt-uni-ui/guess_change.svg',
						//猜你想问文字头部左边图标
						guessIcon: 'https://img.yummall.cn/baiyetong/byt-uni-ui/guess_icon.svg',

						// 发送框左边图标
						sendLeftIcon: 'https://img.yummall.cn/baiyetong/byt-uni-ui/sendLeft233.svg',
						// 发送框右边发送按钮
						sendRightIcon: 'https://img.yummall.cn/baiyetong/byt-uni-ui/sendBtn.svg',
						// 发送框快速提问关闭图标
						sendQuickCloseIcon: 'https://img.yummall.cn/baiyetong/byt-uni-ui/quickIconClose.svg',
						// 发送框快速提问内容左侧图标
						sendQuickContentLeftIcon: 'https://img.yummall.cn/baiyetong/byt-uni-ui/quickIcon1.svg'
					},
					// 头部启动栏欢迎语
					startup: {
						// 头部欢迎栏是否显示
						welcomeShow:true,
						// 欢迎语主题
						tipTitle: '你好，我是洛神AI助手',
            //欢迎语
						tip: '我是洛神AI助手。您有任何关于rosense ai的问题，我可以随时帮您解答。',
						//欢迎语人物头像
						avatar: 'https://img.yummall.cn/baiyetong/byt-uni-ui/role_icon_1.svg', // 1,2
						// 头像回答旋转动效图标
						rotateIcon: 'https://img.yummall.cn/baiyetong/byt-uni-ui/header_icon_outline.png',
						// 欢迎语右边背景图标
						rightIcon: 'https://img.yummall.cn/baiyetong/byt-uni-ui/header_banner_right.png',
						// 欢迎栏的渐变背景色
						backgroundColor:'linear-gradient(90deg, #E7FCF6 0%, #F7FFEE 50%, #DFF8FF 100%);' 

					},
					// 主体内容
					main: {
						// 背景色
						// (1.数字类型， 1-4
						background: 1,
						// (2.对象类型 { type: 'image' | 'linear' | 'coLor' }
						//背景图片
						// background: {
						// 	type: 'image',
						// 	url: "https://img.yummall.cn/baiyetong/byt-uni-ui/role_icon_1.svg", // 1-4
						// },
						//渐变色
						// background: {
						// 	type: 'linear',
						// 	url: "-webkit-linear-gradient(top, #ffffff, #f3f4f8 120rpx)", // 1-4
						// },
						//纯色
						// background: {
						// 	type: 'color',
						// 	url: "rgb(217 224 255)", // 1-4
						// },
						// 背景角色图片
						role: 1 // 1-4
					},
					// 更多工具
					moreList: [{
							id: 1,
							icon: 'https://img.yummall.cn/baiyetong/byt-uni-ui/paishe.svg',
							name: '拍摄'
						},
						{
							id: 2,
							icon: 'https://img.yummall.cn/baiyetong/byt-uni-ui/xiangce.svg',
							name: '相册'
						},
						{
							id: 3,
							icon: 'https://img.yummall.cn/baiyetong/byt-uni-ui/luyin.svg',
							name: '录音'
						},

						{
							id: 4,
							icon: 'https://img.yummall.cn/baiyetong/byt-uni-ui/wendang.svg',
							name: '文件'
						}
					],
					// 更多工具显示开关
					moreListShow: true,
					// 快速提问显示开关
					quickShow: true,

					// 猜你喜欢提示语组件显示开关
					guess: false,
					// 输入框音频识别开关
					audio: true,
					// 基础消息配置
					message: {
						// 回答等待状态提示语
						answerWait: '思考中',
						// 打开组件问候语
						greeting: '您好！有什么可以帮助您的？'
					},
					// 支持的消息类型，目前支持 text、chart，其他类型后续待开发
					typeWhiteList: ['text', 'activity', 'coupon', 'goods', 'chart', 'tables', 'action', 'image', 'FILE', 'TXT'],
					// 消息角色配置
					roles: {
						// AI助手消息配置
						ai: {
							// 有名称就显示，没有就不显示
							name: 'AI助手',
							// 头像
							avatar: 'https://img.yummall.cn/baiyetong/byt-uni-ui/role_icon_1.svg', // 1,2
							// 位置
							placement: 'start'
						},
						// 用户消息配置
						user: {
							name: '用户名称1',
							avatar: '/static/role_icon_1.svg',
							placement: 'end'
						}
					},
					// 业务用户信息
					userInfo: {
						//用户id必传，查询历史消息需要
						userId: '1670672134393978882'
					}
				})
			},
			//ai对话智能体id(必传！！)没有则不可使用
			//1、如果有智能体列表页面（例如listHome页面）的项目，只需要根据token查询平台下的智能体列表，点击对应智能体，将id传给组件即可使用
			//2、如果没有则需要向后端询问查询平台默认智能体对象的接口，在打开对话组件之前先调用获取智能体对象，将id传给组件即可使用
			agentId: {
				type: String,
				default: '',
				required: true
			},
			//对话头部名称
			agentName: {
				type: String,
				default: 'AI助手'
			},
			//整体宽度
			width: {
				type: String,
				default: '100%'
			},
			//整体宽度
			height: {
				type: String,
				default: '100vh'
			},
			// 是否显示头部页面返回按钮
			backShow: {
				type: Boolean,
				default: true
			}
		},
		data() {
			return {
        toolList: [],
        dropdownOptions: [],
        defaultDropdownValue:null,
				// 回答的图片地址数据缓存
				answerImgSrcShow: false,
				answerImgSrc: '',

				// 更多显示
				moreShow: false,

				// 手机状态栏高度
				statusBarHeight: 0,

				// 搜索框工具栏激活功能key数组
				activeList: [],

				// 代理智能体id
        //这块先给个假的
				agentIdAgent: null,
				// 代理智能体名称
				agentNameAgent: '',

				// wx通信重新链接限制
				wxFailureRetryLimit: 3,
				// 重试次数
				wxFailureRetryNum: 0,
				// 重试次数达到限制出现的按钮
				resizeBtnShow: false,

				uuid: '',
				loading: false,
				recordList: this.config.guess ? [{
					type: 3
				}] : [],
				// 查询参数
				queryParams: {
					pagination: {
						pageNum: 1,
						pageSize: 20,
            createOrder: 'desc'
					},
					dialogueHistoryNo: ''
				},
				recordLoading: 'loadmore',
				answerStatus: false,
				answerChart: false,
				inputStyle: {
					height: '80rpx',
					padding: '16rpx 24rpx'
				},
				message: null,
				microphone: false,
				listHeight: '76vh',
				dataList: [],
				socketTask: null,
				// 确保websocket是打开状态
				is_open_socket: false,
				timer: null,
				timerShow: false,
				jumpTimer: null,
				historyId: '',
				contentHeight: '',
				scrollTop: 99999999,
				// 确认是否为大屏
				wideScreenWidth: 700,
				isWideScreen: false,
				// 智能体详情
				aiForm: {},
				taskOrnamentStr: null,
				// 是否显示推荐消息
				showRecommendedMessages: true,
        senderHeight: 0,
        popContent: '',
        windowHeight: '600px',
        deleteparams:{
          chatId: '',
          conversationId: ''
        },
        userData: {}
			};
		},
		computed: {
			// 工具栏是否显示
			toolListShow() {
				return this.toolList && this.toolList.length > 0;
			},
			// 工具栏
			toolListAgent() {
				return this.toolList ? this.toolList : [];
			},
			// 更多工具
			moreListAgent() {
				return this.config.moreList ? this.config.moreList : [];
			},
			// 主题色和图标代理
			colorIconAgent() {
				return this.config.colorIcon ? this.config.colorIcon : {};
			},
			// 消息列表格式化数据
			bubbleItems: function() {
				const list = this.recordList.map((item) => {
					return {
						// 业务数据，可以插槽使用
						...item,
						// key
						key: item.serialNo,
						// 创建时间
						createTime: item.createTime,
						// 角色信息
						role: item.type == 1 ? 'user' : item.type == 2 ? 'ai' : 'slot',
						// 内容
						content: item.content,
						// 内容类型：'text', 'activity', 'coupon', 'goods', 'chart', 'tables', 'action', 'image'
						contentType: item.contentType || 'text',
						// 'chart', 'tables'类型的json数据字符串
						contentAppendData: item.contentAppendData || null
					};
				});

				return list;
			},
			// 助手样式
			aidialogueStyle: function() {
				let {
					background
				} = this.config.main;
				let backgroundKey = 'background-image';
				let backgroundValue = '';
				if (typeof background == 'number') {
					backgroundValue =
						`url('https://img.yummall.cn/baiyetong/byt-uni-ui/bg2${this.isWideScreen ? '' : '_min'}_${background}.png')`;
				} else if (background && typeof background == 'object') {
					if (background.type == 'image') {
						backgroundValue = `url(${background.url})`;
					} else if (background.type == 'linear') {
						backgroundValue = background.url;
					} else if (background.type == 'color') {
						backgroundKey = 'background-color';
						backgroundValue = background.url;
					}
				}
				return `width: ${this.width} ; height: ${this.height};  ${backgroundKey}: ${backgroundValue}`;
			}
		},
		async mounted() {
      this.getSenderHeight()
      uni.$on('voiceSend', (data) => {
        this.message = data;
        this.setMessage()
      });
      uni.$on('edit', (data) => {
        this.message = data;
      });
      uni.$on('selectText', (data) => {
        this.popContent=data
       this.popopen()
      });
      uni.$on('deleteMessage', (data) => {
        this.deleteopenModal(data)
      });
      const _this = this
      await this.getUser()
      await this.getPublished()
      await this.getButtons()
			// if (!this.agentId) {
			// 	// setTimeout(() => {
			// 	//     showToast('必须传入智能体id,否则无法使用对话服务');
			// 	// }, 1000);
			// 	// 请求默认智能体
			// 	await this.getAssistant();
			// } else {
			// 	this.agentIdAgent = this.agentId;
			// 	this.agentNameAgent = this.agentName;
			// }
      //
			// let _this = this;
			// // socket调用
			// await _this.historyAdd();
			// 获取设备宽度
			uni.getSystemInfo({
				success: function(info) {
					console.log('设备的宽度：' + info.windowWidth);
					// 你可以将设备宽度保存在一个变量中供后续使用
          _this.windowHeight = (info.windowHeight-100) +'px'
          console.log('this.windowHeight')
          console.log(this.windowHeight)
					let deviceWidth = info.windowWidth;
					if (deviceWidth >= _this.wideScreenWidth) {
						_this.isWideScreen = true;
					} else {
						_this.isWideScreen = false;
					}
					console.log('当前isWideScreen', _this.isWideScreen);
				}
			});
			// 监听键盘高度变化，以便设置输入框的高度
			//  #ifndef H5
			uni.onKeyboardHeightChange((res) => {
        const _this = this
        _this.config.startup.welcomeShow=false
				console.log('父页面获取高度', res);
        if (res.height > 0) {
          _this.toBottom();
        }
				if (res.height === 0) {
          _this.focus = false;
				}
			});
			// #endif
			// #ifdef H5
			window.addEventListener('resize', _this.onResizeWidth);
			// #endif

			// 获取状态条高度
			const systemInfo = uni.getSystemInfoSync();
			this.statusBarHeight = systemInfo.statusBarHeight;
		},
		// #ifdef VUE2
		beforeDestroy() {
			this.onClear();
      uni.$off('voiceSend');
      uni.$off('edit');
      uni.$off('selectText');
      uni.$off('deleteMessage');
		},
		// #endif
		// #ifdef VUE3
		beforeUnmount() {
			this.onClear();
      uni.$off('voiceSend');
      uni.$off('edit');
      uni.$off('selectText');
      uni.$off('deleteMessage');
		},
		// #endif
		methods: {
      deleteopenModal(data) {
        this.deleteparams = data
        this.$refs.deletemodal.open();
      },
      deletecloseModal() {
        this.$refs.deletemodal.close();
      },
      async deleteconfirm() {
        console.log(this.recordList)
        if (this.answerStatus===true){
          showToast('请等待当前对话完成');
          return
        }
        // 过滤掉对应的对话项
        this.recordList = this.recordList.filter(item => {
          return item.chatId !== this.deleteparams.chatId;
        });
        try {
          // 调用删除接口
          const response = await delete_message(this.deleteparams);

          // 检查接口返回是否成功（假设成功状态码为200）
          if (response.code === 200) {

            // 显示删除成功提示
            // showToast('删除成功');

            // // 如果列表为空，显示欢迎界面
            // if (this.recordList.length === 0) {
            //   this.config.startup.welcomeShow = true;
            // }
          } else {
            // 接口返回失败状态
            showToast(response.msg || '删除失败，请重试');
          }
        } catch (error) {
          // 捕获接口调用异常
          console.error('删除接口调用失败:', error);
          showToast('网络异常，删除失败');
        } finally {
          // 无论成功失败都关闭弹窗
          this.deleteparams.conversationId=''
          this.deleteparams.chatId=''
          this.deletecloseModal();
        }
      },
      popopen(){
        this.$refs.popup.open();
      },
      popchange(e){
        console.log('弹窗状态改变：',e);
      },
      popClose() {
        this.$refs.popup.close();
      },
      getSenderHeight() {
        console.log('开始了');
        this.$nextTick(() => {
          uni.createSelectorQuery().in(this).select('#sender-component').boundingClientRect((rect) => {
            if (rect) {
              this.senderHeight = rect.height;
            } else {
              console.error('未找到 sender 组件元素');
            }
          }).exec();
        });
      },
			isEmpty,
			isJsonString,
			// 当智能体id未传入时，根据token获取默认智能体
      async getUser() {
        try {
          const data = await getInfo();
          this.userData = data
          this.config.roles.user.name = data.user.nickName;

          // 验证头像地址是否有效
          const avatarUrl = address + '/agentapi' + data.user.avatar;
          await verifyImageUrl(avatarUrl).then(() => {
            // 图片加载成功
            this.config.roles.user.avatar = avatarUrl;
          }).catch(() => {
            // 图片加载失败，使用默认头像
            this.config.roles.user.avatar = '/static/role_icon_1.svg';
            console.log('头像加载失败，使用默认头像');
          });

          if (data.code !== 200) {
            await uni.redirectTo({
              url: '/pages/login/index'
            });
          }
        } catch (err) {
          console.log('获取默认智能体报错：', err);
          await uni.showToast({title: '身份信息已过期，请重新登录1', icon: 'none'});
          await uni.redirectTo({
            url: '/pages/login/index'
          });
        }
      },
			// 重新链接
			// wxResize() {
      //   debugger
			// 	showToast('正在重新连接服务器，请稍后再发送');
			// 	this.wxFailureRetryNum = 0;
			// 	this.resizeBtnShow = false;
			// 	this.connectSocketInit();
			// },
      async getPublished(notfirst) {
        console.log(notfirst)
        const _this = this
        try {
          const param = {}
          param.withAttention=-1
          param.withFlow=3
          param.status=1
          const data = await published(param);
          console.log('getPublished开始')
          if (data.code!==200){
            await uni.redirectTo({
              url: '/pages/login/index'
            })
          }
          if (data.data.length!==0) {
            const agents = data.data
            const agentId = agents[0].id
            const agentName = agents[0].name
            const avatar = agents[0].avatar
            const tipTitle = '你好，我是'+agents[0].name
            const tip = agents[0].des
            _this.agentIdAgent = agentId;
            _this.agentNameAgent = agentName;
            _this.config.roles.ai.name = agentName;
            _this.dropdownOptions = agents
            _this.defaultDropdownValue = agentId
            if (notfirst!==true) {
              _this.config.startup.tipTitle=tipTitle
              _this.config.startup.tip=tip
              this.$refs.navbar.selectedValue = agentId
              if (avatar==='/profile'){
                _this.config.roles.ai.avatar = '/static/profile.png';
                _this.config.startup.avatar = '/static/profile.png';
              }else {
                _this.config.roles.ai.avatar = address+'/agentapi'+avatar;
                _this.config.startup.avatar = address+'/agentapi'+avatar;
              }
            }
          }else {
            _this.agentIdAgent = null
            _this.agentNameAgent = null
            _this.dropdownOptions = []
            _this.defaultDropdownValue = null
            this.$refs.navbar.selectedValue = null
          }
        } catch (err) {
          console.log('获取默认智能体报错：', err);
          await uni.showToast({title: '身份信息已过期，请重新登录2', icon: 'none'})
          await uni.redirectTo({
            url: '/pages/login/index'
          })
        }
      },
      async getButtons() {
        const res = await buttons(this.agentIdAgent);
        console.log(res)
        this.toolList = res.data
        // this.config.toolList=data.data
      },
      handleSelectConversation(conversation) {
        this.config.startup.welcomeShow=false
        console.log('父组件接收到选择的对话:', conversation);
        // 设置当前选中的对话ID
        this.queryParams.dialogueHistoryNo = conversation.id;
        // 清空当前消息列表
        this.recordList = [];
        // 重置分页
        this.queryParams.pagination.pageNum = 1;
        // 加载选中对话的历史记录
        this.getData();
        this.toBottom()
      },
      handleDropdownChange(item) {
        const _this = this
        _this.agentIdAgent = item.id
        _this.config.roles.ai.name = item.name;
        _this.config.startup.tipTitle = '你好，我是'+item.name
        _this.config.startup.tip = item.des
        if (item.avatar==='/profile'){
          _this.config.roles.ai.avatar = '/static/profile.png';
          _this.config.startup.avatar = '/static/profile.png';
        }else {
          _this.config.roles.ai.avatar = address+'/agentapi'+item.avatar;
          _this.config.startup.avatar = address+'/agentapi'+item.avatar;
        }
        this.resetconv()
        this.getButtons()
      },
      handleToolListClick(item) {
        this.config.startup.welcomeShow=false
        console.log(item)
        this.message = item
      },
      resetconv(){
        this.message=''
        this.config.startup.welcomeShow=true
        this.queryParams.dialogueHistoryNo = ''
        this.recordList = []
      },
			onClear() {
				// 关闭websocket【必须在实例销毁之前关闭,否则会是underfined错误】
				// #ifdef H5
				window.removeEventListener('resize', this.onResizeWidth);
				// #endif
				// this.closeSocket();
				// //清除chatGPT的上下文内容
				// chatClearMessage({
				// 	agentId: this.agentIdAgent,
				// 	userId: this.config.userInfo.userId
				// });
			},
			onResizeWidth() {
				if (window.innerWidth >= this.wideScreenWidth) {
					this.isWideScreen = true;
				} else {
					this.isWideScreen = false;
				}
				console.log('当前isWideScreen', this.isWideScreen);
			},

			onPull() {
				console.info('用户向上滚动请求历史消息');
				if (this.recordLoading === 'nomore') {
					uni.stopPullDownRefresh();
					return;
				}
				this.queryParams.pagination.pageNum++;
        if (this.queryParams.dialogueHistoryNo!==''&&this.queryParams.dialogueHistoryNo!=='null'&&this.queryParams.dialogueHistoryNo!==null) {
          this.getData(true);
        }
				this.showRecommendedMessages = false
			},
			problemClick(name) {
				this.message = name;
				this.setMessage();
			},
      getData(stopPullDownRefresh = false) {
        this.recordLoading = 'loading';

        messages(this.queryParams)
            .then((res) => {
              console.log('API返回数据:', res);
              let data2 = res.data || [];

              // 按时间排序（最新的在前面）
              data2.sort((a, b) => {
                return new Date(a.id) - new Date(b.id);
              });

              // 处理每条消息
              const processedData = data2.map(item => {
                try {
                  // 解析message字段
                  const messageObj = JSON.parse(item.message || '{}');
                  console.log(messageObj)
                  const excludedTypes = ['ToolError', 'ToolCall', 'ToolResponse', 'ACK'];
                  if (excludedTypes.includes (messageObj.type)) {
                    return null; // 排除这条数据
                  }
                  // 处理不同角色的消息
                  let role = item.role === 'user' ? 'user' : 'ai';

                  // 处理消息内容和类型
                  let content = messageObj.content || item.message;
                  let contentType = messageObj.type || 'text';
                  let type = item.role === 'user' ? 1:2

                  // 格式化时间
                  const formattedTime = this.formatTime(item.createTime);

                  return {
                    ...item,
                    createTime: formattedTime,
                    role,
                    type,
                    content,
                    contentType,
                    // 保留原始数据以便后续使用
                    contentAppendData: messageObj
                  };
                } catch (e) {
                  console.error('消息解析出错:', e);
                  return item;
                }
              });

              // 去除空数据
              const validData = processedData.filter(item => item && item.content);
              console.log('处理后的数据:', validData);

              this.recordList = [...validData, ...this.recordList];
              this.loading = false;

              if (!validData || validData.length < this.queryParams.pagination.pageSize) {
                this.recordLoading = 'nomore';
              } else {
                this.recordLoading = 'loadmore';
              }

              if (stopPullDownRefresh) {
                uni.stopPullDownRefresh();
              }
              console.log('this.recordList')
              console.log(this.recordList)
            })
            .catch((err) => {
              showToast(err.msg || '获取消息失败');
              console.error('获取消息出错:', err);
            });
      },
      formatTime(timeStr) {
        if (!timeStr) return '';

        const date = new Date(timeStr);
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        const hour = String(date.getHours()).padStart(2, '0');
        const minute = String(date.getMinutes()).padStart(2, '0');

        return `${year}-${month}-${day} ${hour}:${minute}`;
      },

			// 终止上一个回复
			stopAnswer() {
				const _this = this;
				console.log('我正在终止回复');
				if (_this.timer) {
					clearInterval(_this.timer);
				}
				_this.answerStatus = false;
				_this.timerShow = false;

				_this.recordList = _this.recordList.filter((item) => item.content?.indexOf(_this.config.message
					.answerWait) !== 0);
				// 向后端发送后就会中断上一次的消息推送
				// let stop = {
				// 	type: 'stop'
				// };
				// _this.socketTask.send({
				// 	data: JSON.stringify(stop),
				// 	success() {
				// 		console.log('终止回复');
				// 	}
				// });
			},
			// 关闭websocket【离开这个页面的时候执行关闭】
			closeSocket() {
				const _this = this;
				if (this.socketTask) {
					this.socketTask.close({
						success(res) {
							_this.is_open_socket = false;
							console.log('关闭成功', res);
						},
						fail(err) {
							console.log('关闭失败', err);
						}
					});
					clearInterval(_this.jumpTimer);
				}
			},
			async setMessage() {
				const _this = this;
        _this.config.startup.welcomeShow=false
				if (_this.showRecommendedMessages) {
					_this.showRecommendedMessages = true; // 确保过渡开始
					this.$nextTick(() => {
						_this.showRecommendedMessages = false; // 触发 Vue 的过渡更新
					});
				}
        //   _this.agentIdAgent=true
				// if (!_this.agentIdAgent) {
				// 	showToast('必须传入智能体id');
				// 	return;
				// }

				if (typeof getToken == 'function') {
					if (!getToken()) {
						showToast('登录已过期，请重新登陆');
            await uni.redirectTo({
              url: '/pages/login/index'
            })
						return;
					}
				} else {
					showToast('getToken必须是函数类型');
					return;
				}

				if (!_this.message || _this.message === '' || _this.message.length <= 0) {
					return;
				}
				if (_this.answerStatus) {
					showToast('请等待当前对话完成');
					return;
				}
        //咱们不是websocket，这块不要了
				// if (_this.is_open_socket === false) {
				// 	console.info('发送消息,socket已关闭,进行重连');
				// 	_this.wxResize();
				// 	return;
				// }
				let message = _this.message;
				_this.message = null;
				_this.microphone = false;
        if (this.queryParams.dialogueHistoryNo===''||this.queryParams.dialogueHistoryNo==='null'||this.queryParams.dialogueHistoryNo==null) {
          console.log('开始获取新会话id')
          const data = await addConversation(this.agentIdAgent);
          this.queryParams.dialogueHistoryNo=data.data.id
        }
				_this.recordList.push({
					createTime: getCurrentDateTime(),
					type: 1,
					content: message,
          conversationId: this.queryParams.dialogueHistoryNo,
				});
				_this.answerStatus = true;
        //这块发请求
				// _this.socketTask.send({
				// 	data: message,
				// 	async success() {
				// 		console.log('消息发送成功');
				// 	}
				// });
				//模拟回答中动画
				_this.recordList.push({
					createTime: getCurrentDateTime(),
					type: 2,
					content: ''
				});
				let lastIndex = _this.recordList.length - 1;

				let msg = _this.config.message.answerWait;
				_this.timer = setInterval(() => {
					_this.timerShow = true;

					let content = _this.recordList[lastIndex].content;
					content = content.replace(msg, '');
					if (content.length >= 3) {
						content = msg + '.';
					} else {
						content = msg + content + '.';
					}
					_this.recordList[lastIndex].content = content;
				}, 300);
        _this.toBottom();
        let chatmsg = {
          agentId: this.agentIdAgent,
          modelId: '',
          message: {
            type: 'TEXT',
            content: message,
            time: getCurrentDateTime()
          },
          conversationId: this.queryParams.dialogueHistoryNo,
          from: 'assistant',
          isPublish: false,
          runType: 'assistant'
        };
        let title = message
        await _this.updateConversationTitle(this.queryParams.dialogueHistoryNo,title)
        await _this.sendMessage(chatmsg)
			},
      async sendMessage(chatmsg){
        const _this = this
        let lastIndex = _this.recordList.length - 1;
        let lastIndexask = _this.recordList.length - 2;
        let str = ''
        let type = 'text'
        let item1 = {}
        let chatId = ''
       await chat(chatmsg).then((res)=>{
         _this.answerStatus = false
         clearInterval(_this.timer);
             res.forEach((item, index, array) => {
               if (item.done===false){
                 if (item.message.type!=='ToolError'&&item.message.type!=='ToolCall'&&item.message.type!=='ToolResponse'&&item.message.type!=='ACK') {
                   str += item.message.content
                   if (item.message.type==='FILE'){
                     type='FILE'
                     item1=item.message
                   }
                 }else if(item.message.type==='ACK'){
                   chatId = item.message.chatId
                 }
               }
             });
           }
       )
        console.log('chatId')
        console.log(chatId)
        _this.recordList[lastIndex].content = str
        _this.recordList[lastIndex].contentType = type
        _this.recordList[lastIndexask].chatId = chatId
        _this.recordList[lastIndex].chatId = chatId
        _this.recordList[lastIndex].conversationId = this.queryParams.dialogueHistoryNo
        _this.recordList[lastIndex].contentAppendData = item1
        _this.toBottom();
      },
			// 显示欢迎信息
			debounceShowGreeting: debounce(function() {
				let _this = this;
				if (this.is_open_socket) {
					let {
						message
					} = _this.config;
					// 如果当前历史记录有提示词，并且上下文没有提示，则进行发送
					if (message.greeting && !_this.recordList.some((item) => item.contentType == 'greeting')) {
						if (!_this.answerStatus) {
							_this.recordList.push({
								createTime: getCurrentDateTime(),
								type: 2,
								content: message.greeting,
								contentType: 'greeting'
							});
						} else {
							console.log('欢迎信息未发送');
						}
					} else {
						console.log('没有提示信息，或者已发送提示');
					}
				}
			}, 500),
			debounceToBottom: debounce(function() {
				this.toBottom();
			}, 100),
			toBottom() {
				this.$nextTick(() => {
					setTimeout(() => {
						this.scrollTop = this.scrollTop + 1;
					}, 100);
				});
			},
			onFocus() {
				this.microphone = false;
			},
			toTest() {
				this.message = '制定一份端午节商城综合体的营销方案';
			},
			historyAdd() {
				// 获取打开一个新的对话的historyId
				chatOpenNewHistory({
						customerNo: this.config.userInfo.userId,
						assistantNo: '1727988700871720962',
						aiAgentId: this.agentIdAgent
					})
					.then((res) => {
						let data = res.data;
						this.historyId = data.serialNo;
						this.queryParams.dialogueHistoryNo = data.serialNo;
						this.connectSocketInit();
					})
					.catch((err) => {
						console.log('------------------');
					});
			},
			// 获取数据json
			getDataJson(data) {
				// 获取json数据
				const {
					taskOrnament,
					description
				} = data;
				let taskOrnamentObj = JSON.parse(taskOrnament || '{}');
				console.log('现在呢', taskOrnamentObj);
				taskOrnamentObj.nodes?.forEach((item) => (item.data = undefined));
				taskOrnamentObj.edges?.forEach((item) => (item.data = undefined));
				this.taskOrnamentStr = JSON.stringify(taskOrnamentObj);
				// 获取首条信息
				// fullWindowMessage.value.tips = description;
				this.recordList.push({
					createTime: getCurrentDateTime(),
					type: 2,
					content: description
				});
				console.log('新数据是', this.taskOrnamentStr, this.recordList);
			},
			getAiDetail() {
				// 获取打开一个新的对话的historyId
				chatTechnologyDetail({
						aiAgentId: this.agentIdAgent
					})
					.then((res) => {
						console.log('数据是', res.data);
						this.getDataJson(res.data);
					})
					.catch((err) => {
						console.log('------------------');
					});
			},
      async updateConversationTitle(conversationId, newTitle) {
        try {
          // 这里需要调用实际的API
          const response = await updateConversation({
            id: conversationId,
            name: newTitle
          });

          if (response.code === 200) {
            // 更新成功，刷新对话列表

          } else {

          }
        } catch (error) {
          console.error('重命名出错', error);
          await uni.showToast({
            title: '网络错误，请重试',
            icon: 'none'
          });
        }
      }
			// onResponseMessage(res) {
			// 	let _this = this;
			// 	clearInterval(_this.timer);
			// 	_this.timerShow = false;

			// 	let lastIndex = _this.recordList.length - 1;
			// 	if (_this.recordList[lastIndex].content.indexOf('AI助手思考中') === 0 || _this
			// 		.recordList[lastIndex].content.indexOf('图片绘制中') === 0) {
			// 		_this.recordList[lastIndex].content = '';
			// 	}
			// 	// 测试结束
			// 	_this.recordList[lastIndex].content = _this.recordList[lastIndex].content + res.answerResult;
			// 	_this.answerStatus = false;
			// 	// 确认数据追加类型
			// 	let {
			// 		chartType,
			// 		chartData
			// 	} = res;
			// 	if (chartType && chartData && !['[]', '{}'].includes(chartData)) {
			// 		console.log("当前数据类型", chartType)
			// 		_this.recordList[lastIndex].contentType = 'chart_' + chartType;
			// 		_this.recordList[lastIndex].contentAppendData = chartData;
			// 	}
			// 	// 测试开始
			// 	// activity, coupon, goods
			// 	// if (lastIndex - 1 >= 0) {
			// 	// 	const preContent = _this.recordList[lastIndex - 1].content;
			// 	// 	if (preContent.includes('活动')) {
			// 	// 		_this.recordList[lastIndex].contentType = 'activity';
			// 	// 	} else if (preContent.includes('优惠券')) {
			// 	// 		_this.recordList[lastIndex].contentType = 'coupon';
			// 	// 	} else if (preContent.includes('商品')) {
			// 	// 		_this.recordList[lastIndex].contentType = 'goods';
			// 	// 	} else if (preContent.includes('图表1')) {
			// 	// 		_this.recordList[lastIndex].contentType = 'chart_1';
			// 	// 	} else if (preContent.includes('图表2')) {
			// 	// 		_this.recordList[lastIndex].contentType = 'chart_2';
			// 	// 	} else if (preContent.includes('图表3')) {
			// 	// 		_this.recordList[lastIndex].contentType = 'chart_3';
			// 	// 	}
			// 	// }
			// },
			// setMessageApi() {
			// 	const _this = this;
			// 	if (!_this.taskOrnamentStr) {
			// 		showToast('请等待当前数据加载');
			// 		return;
			// 	}
			// 	if (!_this.message || _this.message === '' || _this.message.length <= 0) {
			// 		return;
			// 	}
			// 	if (_this.answerStatus) {
			// 		showToast('请等待当前对话完成');
			// 		return;
			// 	}
			// 	let message = _this.message;
			// 	_this.message = null;
			// 	_this.microphone = false;
			// 	_this.recordList.push({
			// 		createTime: getCurrentDateTime(),
			// 		type: 1,
			// 		content: message
			// 	});
			// 	_this.toBottom();
			// 	_this.answerStatus = true;
			// 	//模拟回答中动画
			// 	_this.recordList.push({
			// 		createTime: getCurrentDateTime(),
			// 		type: 2,
			// 		content: '.'
			// 	});
			// 	let lastIndex = _this.recordList.length - 1;

			// 	let msg = 'AI助手思考中';
			// 	// 调用接口
			// 	chatGetAiAgentChat({
			// 			question: message,
			// 			taskOrnament: _this.taskOrnamentStr
			// 		})
			// 		.then((res) => {
			// 			console.log("数据是", res.data)
			// 			this.onResponseMessage(res.data);
			// 		})
			// 		.catch((err) => {
			// 			console.log('请求失败');
			// 		}).finally(() => {
			// 			_this.timerShow = false;

			// 			_this.answerStatus = false;
			// 		});
			// 	// 模拟等待
			// 	_this.timer = setInterval(() => {
			// 		_this.timerShow = true;

			// 		let content = _this.recordList[lastIndex].content;
			// 		content = content.replace(msg, '');
			// 		if (content.length >= 3) {
			// 			content = msg + '.';
			// 		} else {
			// 			content = msg + content + '.';
			// 		}
			// 		_this.recordList[lastIndex].content = content;
			// 	}, 300);
			// }
		}
	};
</script>

<style lang="scss" scoped>
	view {
    -webkit-overflow-scrolling: touch;
		box-sizing: border-box;
	}

	.fade-enter-active,
	.fade-leave-active {
		transition: opacity 0.2s ease;
	}

	.fade-enter-from,
	.fade-leave-to {
		opacity: 0;
	}

	.fade-enter-to,
	.fade-leave-from {
		opacity: 1;
	}

	@include b(aidialogue) {
    -webkit-overflow-scrolling: touch;
		overflow-y: hidden;
		overflow-x: hidden;
		padding-top: 2rpx;
		background-size: 100% 100%;
		position: relative;
		background-position: center center;
		background-repeat: no-repeat;
		background-size: cover;

		@include b(aidialogue-bg) {
			position: relative;
			height: 100%;

			@include e(role) {
				position: absolute;
				bottom: 0;
				left: 0;
				right: 0;
				height: calc(100% - 100rpx);
				z-index: 0;
				margin: 0 auto;
			}

			@include m(float) {
				z-index: 1;
			}
		}

		@include b(aidialogue-header) {
			// background: linear-gradient(90deg, #E7FCF6 0%, #F7FFEE 50%, #DFF8FF 100%);
			background-size: cover !important;
			background-repeat: no-repeat !important;
			margin: 32rpx 32rpx 0;
			border-radius: 24rpx;
			// padding: 32rpx;
		}

		@include b(aidialogue-content) {
			position: relative;
			// padding: 28rpx 0 0;
			z-index: 1;
			border-top-right-radius: 20rpx;
			border-top-left-radius: 20rpx;
			height: calc(100% - 50px - 350rpx - env(safe-area-inset-bottom) - var(--status-bar-height));
			overflow-y: auto;

			@include eb(footer) {
				@include eb(copy) {
					display: flex;
					align-items: center;
					margin-right: 30rpx;

					@include e(text) {
						margin-left: 10rpx;
						color: #1aa0ff;
						font-size: 24rpx;
						white-space: nowrap;

						&.is-action {
							color: #8848cb;
						}
					}
				}
			}
		}

		&.is-default-bg {
			background: #ffffff url('https://img.yummall.cn/wxapp/study/img/ai-assistant-bg2.png') top center no-repeat;
			background-size: contain;

			@include b(aidialogue-content) {
				background: -webkit-linear-gradient(top, #ffffff, #f3f4f8 120rpx);
			}
		}

		@include b(aidialogue-tabbar) {
			// #ifndef H5
			height: calc(120rpx + env(safe-area-inset-bottom));
			// #endif
			// #ifdef H5
			height: calc(160rpx);
			// #endif
			position: relative;

			@include e(box) {
				position: absolute;
				bottom: 0;
				left: 0;
				right: 0;
				z-index: 99;
				border-radius: 12px 12px 0px 0px;

				&.is-min {
					background: rgba(255, 255, 255, 0.7);
				}
			}

			@include e(content) {
				max-width: 1800rpx;
				margin: 0 auto;

				@include eb(input) {
					display: flex;
					align-items: center;
					padding: 16rpx 24rpx;

					@include e(audio) {
						margin-right: 28rpx;
					}

					@include e(textarea) {
						flex-grow: 1;
						min-height: 80rpx;
						border-radius: 88rpx;
						border: 4rpx solid transparent;
						// background-clip: padding-box, border-box;
						// background-origin: padding-box, border-box;
						// background-image: linear-gradient(to right, #fff, #fff), linear-gradient(to left, #5ee1fcff, #9caaf6ff, #9e8cf6ff, #5d8bfdff);
						background: rgba(255, 255, 255, 0.8);
						border: 2px solid #9e8cf6ff;
						border-radius: 44rpx;
						box-sizing: content-box;
						display: flex;
						justify-content: space-between;
						align-items: center;

						@include e(inner) {
							min-height: 48rpx;
							max-height: 500rpx;
							padding: 16rpx 24rpx;
							box-sizing: content-box;
							border-radius: 80rpx;
							width: calc(100% - 32rpx - 80rpx);
							line-height: 48rpx;
							font-size: 28rpx;

							// &::placeholder {
							// 	line-height: 48rpx;
							// }
						}

						@include e(confirm) {
							width: 56rpx;
							height: 56rpx;
							margin-right: 18rpx;
							background-color: #26a5ff;
							border-radius: 50%;
							display: flex;
							justify-content: center;
							align-items: center;
						}
					}
				}
			}
		}
	}

  .b-popup{
    padding: 16rpx;
    height: 100%;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    margin-bottom: 16rpx;
  }
</style>